#include "stdafx.h"

#include "LookFeelSkinEd.h"
#include "LookFeelEditor.h"


LookFeelSkinEd::LookFeelSkinEd()
	: mEnableDrag(false)
{
	mLayout = new MGUI_Layout(NULL);

	mImageBox_Back = new MGUI_ImageBox(mLayout);
	mImageBox_Back->SetSkin("BackgroundTile.png");
	mImageBox_Back->SetPickFlag(MGUI_PickFlag::None);

	mImageBox_Skin = new MGUI_ImageBox(mLayout);
	mImageBox_Skin->SetOrder(1);

	mRectEd = new RectEd(mImageBox_Skin);

	LookFeelEditor::Instance()->eventEditSkinChanged += OnEditSkinChanged(this, &LookFeelSkinEd::OnEditSkinChanged_);
	LookFeelEditor::Instance()->eventEditRectChanged += OnEditRectChanged(this, &LookFeelSkinEd::OnEditRectChanged_);
	LookFeelEditor::Instance()->eventEditClientChanged += OnEditClientChanged(this, &LookFeelSkinEd::OnEditClientChanged_);

	mRectEd->evRectChanged += OnRectEdChanged(this, &LookFeelSkinEd::OnRectEdChanged_);

	mImageBox_Skin->eventMousePressed += OnSkinMousePressed(this, &LookFeelSkinEd::OnSkinMousePressed_);
	mImageBox_Skin->eventMouseReleased += OnSkinMouseReleased(this, &LookFeelSkinEd::OnSkinMouseReleased_);
	mImageBox_Skin->eventMouseMove += OnSkinMouseMove(this, &LookFeelSkinEd::OnSkinMouseMove_);
}

LookFeelSkinEd::~LookFeelSkinEd()
{
	delete mLayout;
}

void LookFeelSkinEd::Layout()
{
	MGUI_Rect rect;

	rect.x0 = 256;
	rect.y0 = 32;
	rect.x1 = MGUI_Engine::Instance()->GetRect().x1 - 256;
	rect.y1 = MGUI_Engine::Instance()->GetRect().y1 - 32;

	mLayout->SetRect(rect);
}

void LookFeelSkinEd::OnEditSkinChanged_(Texture * _skin)
{
	mImageBox_Skin->SetSkin(_skin);

	if (_skin)
	{
		int skinW = _skin->GetWidth();
		int skinH = _skin->GetHeight();
		int backW = mImageBox_Back->GetSkin()->GetWidth();
		int backH = mImageBox_Back->GetSkin()->GetHeight();

		mImageBox_Back->SetRect(0, 0, skinW - 1, skinH - 1);
		mImageBox_Skin->SetRect(0, 0, skinW - 1, skinH - 1);

		mImageBox_Back->SetUVRect(MGUI_RectF(0, 0, skinW / (float)backW, skinH / (float)backH));
	}
	else
	{
		mImageBox_Back->SetRect(0, 0, 0, 0);
		mImageBox_Skin->SetRect(0, 0, 0, 0);
	}
}

void LookFeelSkinEd::OnEditRectChanged_(MGUI_Rect _rect)
{
	mRectEd->SetEditRect(_rect);
}

void LookFeelSkinEd::OnEditClientChanged_(MGUI_ClientRect _rect)
{
	mRectEd->SetEditClient(_rect);
}

void LookFeelSkinEd::OnRectEdChanged_(MGUI_Rect _rect)
{
	LookFeelEditor::Instance()->_notifyRectChanged(_rect);
}

void LookFeelSkinEd::OnSkinMousePressed_(int _x, int _y, MGUI_MouseButton _id)
{
	if (_id == MGUI_MouseButton::Right)
	{
		mTmpRect = mImageBox_Skin->GetRect();
		mTmpX = _x;
		mTmpY = _y;
		mEnableDrag = true;

		MGUI_InputManager::Instance()->SetForceFocus(true);
	}
}

void LookFeelSkinEd::OnSkinMouseReleased_(int _x, int _y, MGUI_MouseButton _id)
{
	if (_id == MGUI_MouseButton::Right)
	{
		mEnableDrag = false;
		MGUI_InputManager::Instance()->SetForceFocus(false);
	}
}

void LookFeelSkinEd::OnSkinMouseMove_(int _x, int _y)
{
	if (!mEnableDrag)
		return ;

	int dx = _x - mTmpX;
	int dy = _y - mTmpY;

	MGUI_Rect rc = mTmpRect;

	rc.x0 += dx, rc.y0 += dy;
	rc.x1 += dx, rc.y1 += dy;

	int rdx = rc.x1 - rc.x0;
	int rdy = rc.y1 - rc.y0;

	if (rc.x0 > 0)
	{
		rc.x0 = 0;
		rc.x1 = rdx;
	}

	if (rc.y0 > 0)
	{
		rc.y0 = 0;
		rc.y1 = rdy;
	}
		
	mImageBox_Back->SetRect(rc);
	mImageBox_Skin->SetRect(rc);
}

